
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.PayRecord;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:查看充值记录请求类
 * 
 * @author liux
 * @since 2013-12-9 上午11:24:25
 */
public class ChargeHistoryTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 查看充值记录
     * 
     * @param merchantId
     * @param appId
     * @param userName
     * @param appKey
     * @return
     */
    public CheckCostRecordsResponse request(String userName) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        CheckCostRecordsResponse respObj = new CheckCostRecordsResponse();
        ResponsePackage resp = new CheckCostRecordsResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_CONSUME_RECORDS);
        params.put("username", userName);
        // KGLog.d("kugou", merchantId + " " + appId + " " +
        // UrlEncodeUtil.encode(userName, "gbk")
        // + "  " + AppUtil.getMd5Str(merchantId, appId));
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_CONSUME_RECORDS);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    // 响应
    class CheckCostRecordsResponsePackage extends HttpBaseResponsePackage<CheckCostRecordsResponse> {

        private ArrayList<PayRecord> mArrayList = new ArrayList<PayRecord>();

        @Override
        protected void handleResponse(CheckCostRecordsResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "ChargeHistoryTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_CONSUME_RECORDS.equalsIgnoreCase(cmd)) {
                        JSONArray jsonResult = jsonResp.getJSONArray("data");
                        if (jsonResult != null) {
                            for (int j = 0; j < jsonResult.length(); j++) {
                                JSONObject jsonObject = jsonResult.getJSONObject(j);
                                String mPayType = jsonObject.getString("PayType");
                                String mMoney = jsonObject.getString("Money");
                                String mPayOption = jsonObject.getString("PayOption");
                                String mPayTime = jsonObject.getString("PayTime");
                                PayRecord record = new PayRecord();
                                record.setPayType(mPayType);
                                record.setMoney(mMoney);
                                record.setPayOption(mPayOption);
                                record.setPayTime(mPayTime);
                                mArrayList.add(record);
                            }
                        }
                        response.setArrayListData(mArrayList);
                    }
                }
            } catch (Exception e) {
            }

        }

    }

    /**
     * 消费记录实体类
     * 
     * @author chenys
     */
    public static class CheckCostRecordsResponse extends ResponseMessage {

        private ArrayList<PayRecord> data = new ArrayList<PayRecord>();

        public void setArrayListData(ArrayList<PayRecord> data) {

            this.data = data;
        }

        public ArrayList<PayRecord> getArrayListData() {
            return data;
        }
    }

}
